package com.shujia.flink.core

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo5ProcTime {
  def main(args: Array[String]): Unit = {

    /**
     * 每隔5秒统计单词的数量，统计最近5秒的数据
     */
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val wordsDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = wordsDS.map(word => (word, 1))

    /**
     * TumblingProcessingTimeWindows: 滑动的处理时间窗口
     */
    kvDS
      .keyBy(_._1)
      .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
      .sum(1)
      .print()

    env.execute()
  }
}
